package net.cl.tools;

/**
 * 对象工厂类
 * 
 * @author caol
 */
@Deprecated
public class ObjectFactory {

	private static ClassLoader externalClassLoader;

	private static ClassLoader getClassLoader() {
		if (externalClassLoader != null)
			return externalClassLoader;
		else
			return Thread.currentThread().getContextClassLoader();
	}

	private static Class<?> externalClassForName(String type) throws ClassNotFoundException {
		Class<?> clazz;
		try {
			clazz = getClassLoader().loadClass(type);
		} catch (Throwable e) {
			clazz = null;
		}
		if (clazz == null)
			clazz = Class.forName(type);
		return clazz;
	}

	public static Object createExternalObject(String type) {
		Object obj;
		try {
			Class<?> clazz = externalClassForName(type);
			obj = clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(type, e);
		}
		return obj;
	}

	public static Object createInternalObject(String type) {
		Class<?> clazz = null;
		try {
			ClassLoader cl = Thread.currentThread().getContextClassLoader();
			clazz = cl.loadClass(type);
		} catch (Exception e) {
		}
		Object obj;
		try {
			if (clazz == null)
				clazz = Class.forName(type);
			obj = clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(type, e);
		}
		return obj;
	}

}
